package com.bruce.ai.alibaba.agentcard.register.controller;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;

/**
 * Agent Card 控制器
 * 提供 Agent 的基本信息
 */
@RestController
public class AgentCardController {

    @Value("${agent.service-name:asurada-agent}")
    private String serviceName;

    @Value("${agent.description:Asurada Agent for A2A communication}")
    private String description;

    @Value("${agent.version:1.0.0}")
    private String version;

    @Value("${agent.skills:general,testing}")
    private String skills;

    @Value("${server.port:8080}")
    private int serverPort;

    /**
     * 获取 Agent Card 信息
     */
    @GetMapping("/agentcard")
    public Map<String, Object> getAgentCard() {
        Map<String, Object> agentCard = new HashMap<>();
        agentCard.put("name", serviceName);
        agentCard.put("description", description);
        agentCard.put("version", version);
        agentCard.put("skills", skills.split(","));
        agentCard.put("provider", "Asurada");
        agentCard.put("port", serverPort);
        agentCard.put("endpoints", Map.of(
            "agentcard", "/agentcard",
            "a2a", "/a2a/call",
            "health", "/actuator/health"
        ));
        return agentCard;
    }

    /**
     * A2A (Agent-to-Agent) 调用端点
     * 符合 A2A 协议标准的响应格式
     */
    @PostMapping("/a2a/call")
    public Map<String, Object> a2aCall(@RequestBody Map<String, Object> request) {
        long startTime = System.currentTimeMillis();
        
        Map<String, Object> response = new HashMap<>();
        
        // 从请求中提取信息
        String requestId = (String) request.get("id");
        String sourceAgent = (String) request.get("source_agent");
        String targetAgent = (String) request.get("target_agent");
        String message = (String) request.get("message");
        
        // 构建符合 A2A 协议的响应
        response.put("id", java.util.UUID.randomUUID().toString());
        response.put("request_id", requestId);
        response.put("timestamp", java.time.Instant.now().toString());
        response.put("source_agent", targetAgent); // 响应时角色互换
        response.put("target_agent", sourceAgent); // 响应时角色互换
        response.put("status", "SUCCESS");
        response.put("message", "Hello from " + serviceName + "! Received: " + message);
        response.put("processing_time_ms", System.currentTimeMillis() - startTime);
        
        // 添加一些元数据
        Map<String, Object> metadata = new HashMap<>();
        metadata.put("agent_name", serviceName);
        metadata.put("agent_version", version);
        metadata.put("processed_at", java.time.Instant.now().toString());
        response.put("metadata", metadata);
        
        return response;
    }
}